import { createRouter, createWebHistory } from 'vue-router'
import routersConfig from './config'
import store from '../store/index'
import Login from '../views/Login.vue'
import MainBox from '../views/MainBox.vue'
import NotFound from '../views/NotFound.vue'
const routes = [
{
  path: '/login',
  name: 'login',
  component: Login
},
{
  path:'/',
  redirect:'/login'
},
{
  path: '/mainbox',
  name: 'mainbox',
  component: MainBox
},

{
  path:'/:pathMatch(.*)*',
  name:'NotFound',
  component:NotFound
}
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由拦截
// router.beforeEach((to, from, next) => {
//   if (to.path === '/login') {
//     return next()
//   }else{
//     // 如果未授权就重定向的login
//     if(!localStorage.getItem('token')){
//       next({
//         path:'/login'
//       })
//     }else{
//       // 是否是第一次登陆
//       if(!store.state.isFirstRouter){
//         configRouter()
//         next({
//           path:to.fullPath
//         })
//       }
//       next()
//     }
//   }
// })

// 动态添加路由router.add({'父组件名'，{子组件路由}})
// const configRouter = ()=>{
  routersConfig.forEach(item => {
    router.addRoute('mainbox',item)
  })
  // 改变登陆状态
  // store.commit('changeFirstRouter',true)

// }

export default router
